package com.hunray.rent.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.Length;
@Entity
@Table(name="t_permission")
public class Permission implements Serializable {

	private static final long serialVersionUID = 1L;
	
	private Integer id;
	
	private String no;
	
	private String name;//中文名称
	
	private String identityName; //用于程序判定的唯一英文标识
	
	private String descr;
	
	private Resource resource;
	
	private List<PermissionOperation> permissionOperations=new ArrayList<PermissionOperation>();
	
	private List<RolePermission> rolePermissions=new ArrayList<RolePermission>();
	@Id
	@GeneratedValue(strategy=GenerationType.TABLE,generator="tg_permission")
	@TableGenerator(name="tg_permission",table="t_id",pkColumnName="f_table",pkColumnValue="t_permission",valueColumnName="f_id",allocationSize=1,initialValue=1)
	@Column(name="f_id")
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}
	
	@Column(name="f_no",nullable=false,unique=true)
	@NotNull
	public String getNo() {
		return no;
	}

	public void setNo(String no) {
		this.no = no;
	}
	
	@Column(name="f_name",nullable=false,unique=true,length=100)
	@NotNull
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	@Column(name="f_identity_name",nullable=false,unique=true,length=100)
	@NotNull
	public String getIdentityName() {
		return identityName;
	}

	public void setIdentityName(String identityName) {
		this.identityName = identityName;
	}
	
	@Column(name="f_descr",length=1024)
	@Length(max=1024)
	public String getDescr() {
		return descr;
	}

	public void setDescr(String descr) {
		this.descr = descr;
	}
	@OneToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="f_resource")
	public Resource getResource() {
		return resource;
	}

	public void setResource(Resource resource) {
		this.resource = resource;
	}
	@OneToMany(fetch=FetchType.LAZY,mappedBy="permission")
	public List<PermissionOperation> getPermissionOperations() {
		return permissionOperations;
	}

	public void setPermissionOperations(
			List<PermissionOperation> permissionOperations) {
		this.permissionOperations = permissionOperations;
	}
	@OneToMany(fetch=FetchType.LAZY,mappedBy="permission")
	public List<RolePermission> getRolePermissions() {
		return rolePermissions;
	}

	public void setRolePermissions(List<RolePermission> rolePermissions) {
		this.rolePermissions = rolePermissions;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Permission other = (Permission) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Permission [name=" + name + ", identityName=" + identityName
				+ "]";
	}

}
